/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.internal.panel;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.genia.toolbox.basics.editor.gui.view.internal.panel.ElementViewPanel;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.internal.FormDocumentInternalFrame;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.bean.FormInitialSettings;
import com.genia.toolbox.form.editor.model.form.impl.ContainerFormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;
import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The tab form container view panel.
 */
@SuppressWarnings("serial")
public class TabFormViewPanel
    extends AbstractContainerFormViewPanel
    implements ChangeListener
{

  /**
   * The add tab menu.
   */
  private JMenuItem menuAddTab = null;

  /**
   * The tabbed pane.
   */
  private JTabbedPane tabbed = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The gui.
   * @param internalFrame
   *          The internal frame.
   */
  public TabFormViewPanel(FormEditorGUI gui, FormDocumentInternalFrame internalFrame)
  {
    super(gui, internalFrame);
  }



  /**
   * Create the element type.
   * 
   * @return the element type.
   */
  @Override
  public FormType getElementType()
  {
    return FormType.TAB;
  }



  /**
   * Create the common panel.
   * 
   * @return the common panel.
   */
  @Override
  public JPanel createCommonPanel()
  {
    JPanel panel = super.createCommonPanel();
    this.getLabelType().setVisible(true);
    this.getLabelId().setVisible(false);
    return panel;
  }



  /**
   * Display the sub-elements.
   */
  @Override
  protected void displaySubElements()
  {
    if (this.tabbed != null) {
      this.tabbed.removeChangeListener(this);
    }
    this.getSpecificPanel().removeAll();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    this.getSpecificPanel().setLayout(layout);

    int index = 0;
    int selectedIndex = index;
    this.tabbed = new JTabbedPane();
    for (ElementViewPanel<FormEditorGUI, FormDocumentInternalFrame, FormModel, FormType> panel : this.getSubViewPanels()) {
      this.tabbed.add(panel.getElement().getName(), (JPanel) panel);
      if (panel.hasAtLeastOneCommonSelected()) {
        selectedIndex = index;
      }
      index++;
    }

    this.getSpecificPanel().add(this.tabbed, constraints);

    this.getSpecificPanel().revalidate();
    this.revalidate();
    this.repaint();

    this.tabbed.setSelectedIndex(selectedIndex);
    this.tabbed.addChangeListener(this);

  }



  /**
   * Create the popup menu.
   * 
   * @return the popup menu.
   */
  @Override
  public JPopupMenu createPopupMenu()
  {
    JPopupMenu popup = super.createPopupMenu();
    this.getMenuAdd().setEnabled(false);
    this.getMenuAddForm().setEnabled(false);

    this.menuAddTab = new JMenuItem(this.getGui().getController().notifyTranslation(Messages.VIEW_PANEL_POPUP_MENU_ADD_TAB));
    this.menuAddTab.addActionListener(this);
    popup.add(this.menuAddTab, 2);

    return popup;
  }



  /**
   * Display the popup.
   * 
   * @param component
   *          The component.
   * @param x
   *          The x position.
   * @param y
   *          The y position.
   */
  @Override
  public void displayPopup(Component component, int x, int y)
  {
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    int nbDisplayedSubElements = containerElementModel.getNbSubForms();
    this.getMenuAdd().setEnabled(false);
    this.getMenuAddForm().setEnabled(false);
    if (nbDisplayedSubElements >= containerElementModel.getMaxNbSubForms()) {
      this.getMenuAddTab().setEnabled(false);
    }
    else {
      this.getMenuAddTab().setEnabled(true);
    }
    if (containerElementModel.isSubElement()) {
      this.getMenuDelete().setEnabled(true);
      this.getMenuDown().setEnabled(true);
      this.getMenuUp().setEnabled(true);
      if (LayoutDirection.HORIZONTAL.equals(containerElementModel.getParentDirection())) {
        this.getMenuDown().setEnabled(false);
        this.getMenuUp().setEnabled(false);
        this.getMenuRight().setEnabled(true);
        this.getMenuLeft().setEnabled(true);
      }
      else {
        this.getMenuDown().setEnabled(true);
        this.getMenuUp().setEnabled(true);
        this.getMenuRight().setEnabled(false);
        this.getMenuLeft().setEnabled(false);
      }
    }
    else {
      this.getMenuDelete().setEnabled(false);
      this.getMenuDown().setEnabled(false);
      this.getMenuUp().setEnabled(false);
      this.getMenuRight().setEnabled(false);
      this.getMenuLeft().setEnabled(false);
    }
    this.getPopup().show(component, x, y);
  }



  /**
   * Get the add tab menu.
   * 
   * @return the add tab menu.
   */
  public JMenuItem getMenuAddTab()
  {
    return menuAddTab;
  }



  /**
   * Handle action events.
   * 
   * @param e
   *          the action event.
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    super.actionPerformed(e);
    Object source = e.getSource();
    if (source.equals(this.menuAddTab)) {
      this.addTab();
    }
  }



  /**
   * Add a tab.
   */
  public void addTab()
  {
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    FormInitialSettings settings = this.getGui().createTab();
    this.getGui().getController().notifyCreateTab(settings, containerElementModel.getDocumentID(), containerElementModel.getUniqueID(), containerElementModel.getDirection());
  }



  /**
   * Handle the tab pane change event.
   * 
   * @param e
   *          The tab pane event.
   */
  public void stateChanged(ChangeEvent e)
  {
    int index = this.tabbed.getSelectedIndex();
    this.deactivateParent();
    for (ElementViewPanel<FormEditorGUI, FormDocumentInternalFrame, FormModel, FormType> panel : this.getSubViewPanels()) {
      panel.deactivateAll();
    }
    ElementViewPanel<FormEditorGUI, FormDocumentInternalFrame, FormModel, FormType> panel = this.getSubViewPanels().get(index);
    panel.activateCommon(true);
    this.getGui().displayElementSettings(panel.getElement());
  }

}
